<?php
//======================================================================================
//
// Function: Save customer data
//
// Programmer: JKJ
// Date : 2025-05-02
//
//======================================================================================
// Return codes
//======================================================================================
// Code Description
// 99 No data found
// 98 Mode is invalid (*add/*update/*delete)
// 97 Token is invalid
// 96 Token not found on reference
// 95 No pk_dashboard passed to program, cannot update - if allowed, see const may_I_Update
// 94 No pk_dashboard passed to program, cannot delete - if allowed, see const may_I_Delete
// 93 Input is not valid jSon
// 92 Invalid dashboard type, cannot be blank
// 91 Invalid dashboard type
//
// ------------------------------------
// Dashboard types - overview
// ------------------------------------
// Table name Type
// ------------------------------------
// visual_dashboard_angle 5001
// visual_dashboard_flow 5002
// visual_dashboard_position 5003
// visual_dashboard_rpm 5004
// visual_dashboard_sound 5005
// visual_dashboard_temperature 5006
// visual_dashboard_power 5007
// visual_dashboard_temperature 5008
// visual_dashboard_rpm 5009
// ------------------------------------
//
//
// Copyright Reeft A/S (c) - 2023
//======================================================================================
//======================================================================================
// Set start time
//======================================================================================
$starttime = microtime(true);
//======================================================================================
// Get input
//======================================================================================
if (isset($_REQUEST["dashboard_type"])) $dashboard_type = $_REQUEST["dashboard_type"];
else $dashboard_type = '';
if (isset($_REQUEST["pk_dashboard"])) $pk_dashboard = $_REQUEST["pk_dashboard"];
else $pk_dashboard = 0;
if (isset($_REQUEST["dashboard_comment"])) $input_dashboard_comment = $_REQUEST["dashboard_comment"];
else $input_dashboard_comment = '';
if (isset($_REQUEST["dashboard_bookmark"])) $dashboard_bookmark = $_REQUEST["dashboard_bookmark"];
else $dashboard_bookmark = '';
if (isset($_REQUEST["dashboard_image"])) $dashboard_image = $_REQUEST["dashboard_image"];
else $dashboard_image = '';
//======================================================================================
// JSON input
//======================================================================================
//======================================================================================
// General config
//======================================================================================
include "../config/config.php";
include "../config/dashboard_data_config.php";
include "../config/dashboard_data_config_user.php";
include "../include/uuid_create.php";
include "../include/REEFT_date_convert.php";
//======================================================================================
// Init - just in case (temp defaults)
//======================================================================================
//======================================================================================
// Set header and likewise
//======================================================================================
header('Content-Type: application/json;charset=utf-8');
$currentDate = date('Y-m-d');
$currentTime = date('H:i:s');
$returnCode = '99';
$returnMsg = 'No data found';
$current_elm = 0;
$save_mode = 0;
$table_name_to_use = '';
$table_name_to_use_disk = '';
//======================================================================================
// Check type
//======================================================================================
if ( $dashboard_type == '' ) {
$returnCode = '92';
$returnMsg = 'Invalid dashboard type, cannnot be blank ';
$validToken = 0;
}
else if (
$dashboard_type <> 5001 and
$dashboard_type <> 5002 and
$dashboard_type <> 5003 and
$dashboard_type <> 5004 and
$dashboard_type <> 5005 and
$dashboard_type <> 5006 and
$dashboard_type <> 5007 and
$dashboard_type <> 5008 and
$dashboard_type <> 5009
)
{
$returnCode = '91';
$returnMsg = 'Invalid dashboard type: ' . $dashboard_type;
$validToken = 0;
}
//======================================================================================
// Set table according to type
//======================================================================================
// visual_dashboard_angle 5001
// visual_dashboard_flow 5002
// visual_dashboard_position 5003
// visual_dashboard_rpm 5004
// visual_dashboard_sound 5005
// visual_dashboard_temperature 5006
// visual_dashboard_power 5007
// visual_dashboard_temperature 5008
// visual_dashboard_rpm 5009
//======================================================================================
if ( $dashboard_type == '5001' ) {
$table_name_to_use = 'visual_dashboard_angle';
}
else if ( $dashboard_type == '5002' ) {
$table_name_to_use = 'visual_dashboard_flow';
}
else if ( $dashboard_type == '5003' ) {
$table_name_to_use = 'visual_dashboard_position';
}
else if ( $dashboard_type == '5004' ) {
$table_name_to_use = 'visual_dashboard_rpm';
}
else if ( $dashboard_type == '5005' ) {
$table_name_to_use = 'visual_dashboard_sound';
}
else if ( $dashboard_type == '5006' ) {
$table_name_to_use = 'visual_dashboard_temperature';
}
else if ( $dashboard_type == '5007' ) {
$table_name_to_use = 'visual_dashboard_power';
}
else if ( $dashboard_type == '5008' ) {
$table_name_to_use = 'visual_dashboard_temperature';
}
else if ( $dashboard_type == '5009' ) {
$table_name_to_use = 'visual_dashboard_rpm';
}
else {
$table_name_to_use = 'visual_dashboard__junk_data';
}
//======================================================================================
// Connect to some database
//======================================================================================
include "../include/db_connect_dir.php";
//======================================================================================
// Add quoutes to string
//======================================================================================
$dashboard_comment = $file_db->quote($input_dashboard_comment);
//======================================================================================
// Defaults / inits
//======================================================================================
//======================================================================================
// Begin commit
//======================================================================================
$sql = 'BEGIN TRANSACTION';
include "../include/db_run_sql.php";
//======================================================================================
// Create SQL
//======================================================================================
//======================================================================================
// Update
//======================================================================================
$mode = '*update';
if ( $mode == '*update' )
{
if ( $pk_dashboard == 0 ) {
$returnCode = '95';
$returnMsg = 'No pk_dashboard passed to program, cannot update';
}
$sql = "update $table_name_to_use
set
dashboard_comment = $dashboard_comment,
dashboard_bookmark = '$dashboard_bookmark',
dashboard_image = '$dashboard_image'
where pk_dashboard = $pk_dashboard
";
}
//echo $sql;
//======================================================================================
// mySQL or SQLite (default)
//======================================================================================
if ( $mode <> '*error' )
{
include "../include/db_run_sql.php";
if ( $current_elm == 0 ) {
} else {
$returnCode = '00';
$returnMsg = 'Alles ist gut';
}
} else {
$returnCode = '98';
$returnMsg = 'Mode is invalid: ' . $save_mode;
}
// Save sql if debug might come up?
$save_sql = $sql;
//======================================================================================
// End commit
//======================================================================================
$sql = 'COMMIT';
include "../include/db_run_sql.php";
@$last_id = $file_db->lastInsertId();
//======================================================================================
// Calculate response time
//======================================================================================
$endtime = microtime(true);
$response_time = $endtime - $starttime;
$response_time = number_format($response_time, 6, '.', '');
$response_time_raw = number_format($response_time, 6, '.', '');
$response_time = '(' . $response_time . ' seconds)';
$response_time_raw = $response_time_raw;
//======================================================================================
// Create header
//======================================================================================
$aryHeader = array();
$aryHeader["returnCode"] = $returnCode;
$aryHeader["returnMsg"] = $returnMsg;
$aryHeader["currentDate"] = $currentDate;
$aryHeader["currentTime"] = $currentTime;
$aryHeader["entries_found"] = (int)$current_elm;
$aryHeader["mode"] = $mode;
$aryHeader["dashboard_type"] = $dashboard_type;
$aryHeader["table_name_to_use"] = $table_name_to_use;
$aryHeader["input_mode"] = $save_mode;
$aryHeader["pk_dashboard"] = $pk_dashboard;
$aryHeader["response_time"] = $response_time;
$aryHeader["response_time_raw"] = $response_time_raw;
//======================================================================================
// Create array and prepare for json encoding
//======================================================================================
$returnJson["header"] = $aryHeader;
//======================================================================================
// Paint it black
//======================================================================================
echo(json_encode($returnJson));
?>